home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / Recursive Shell 1.0.1 / Examples / Count Items ƒ / DoStuff.c < prev    next >
Text File  |  1996-06-17  |  6KB  |  240 lines

  1. /********************************************************************************/
  2. //
  3. //    DoStuff.c
  4. //
  5. //    Part of a shell program, this is the file that will be changed to make it do
  6. //    what it's suppossed to do.
  7. //
  8. /********************************************************************************/
  9.  
  10.  
  11. #include    "DialogUtil.h"
  12. #include    "DoStuff.h"
  13. #include    "Utility.h"
  14.  
  15.  
  16. //    Global variables
  17.  
  18. long    gFolderCount, gFileCount;
  19.  
  20.  
  21. //    Prototypes
  22.  
  23. void    reverse( char s[] );
  24. void    itoa( long n, char s[] );
  25. long    strlen(char *str);
  26. void    CtoP( char* cString, Str255 pString );
  27. void    PtoC( Str255 pString, char* cString );
  28.  
  29.  
  30. //    Routines that will be called by the other parts of this program:
  31. //    (Each of these must be present, and their names must be unchanged)
  32. //
  33. //    InitializeStuff
  34. //    DoToEachFile
  35. //    DoToEachFolder
  36. //    DoToSelFolder
  37. //    DeInitializeStuff
  38.  
  39.  
  40. /********************************************************************************/
  41. //
  42. //    InitializeStuff is a routine which gets called before anything else happens.
  43. //    This allows the opportunity to set global variables, read preferences, etc.
  44. //    that will affect how the other parts of this file behave.
  45. //    
  46. //    Expected to return true if initialization succeeded and we should continue
  47. //    processing files.
  48. //
  49. /********************************************************************************/
  50.  
  51. Boolean    InitializeStuff( void )
  52. {
  53. //    Get the name of a text file from the user and create or open it.
  54. //    Store the reference to it in a global variable for later use.
  55.  
  56.     gFolderCount = 0;
  57.     gFileCount = 0;
  58.     return ( true );
  59. }
  60.  
  61.  
  62. /********************************************************************************/
  63. //
  64. //    DoToEachFile is a routine which is called for each file the recursion routine
  65. //    encounters.
  66. //
  67. /********************************************************************************/
  68.  
  69. void    DoToEachFile( FSSpec fileSpec )
  70. {
  71.     gFileCount++;
  72. }
  73.  
  74.  
  75. /********************************************************************************/
  76. //
  77. //    DoToEachFolder is a routine which is called for each file the recursion
  78. //    routine encounters.
  79. //
  80. /********************************************************************************/
  81.  
  82. void    DoToEachFolder( FSSpec folderSpec )
  83. {
  84.     gFolderCount++;
  85. }
  86.  
  87.  
  88. /********************************************************************************/
  89. //
  90. //    If the user dropped a file on to the icon, after all calls to DoToEachFile
  91. //    and DoToEachFolder are completed, DoToSelFile is called with a reference to
  92. //    the file the user dropped.
  93. //
  94. /********************************************************************************/
  95.  
  96. void    DoToSelFile( FSSpec    fileSpec )
  97. {
  98.     gFileCount++;
  99. }
  100.  
  101.  
  102. /********************************************************************************/
  103. //
  104. //    If the user selected a folder or dropped one on to the icon, after all calls
  105. //    to DoToEachFile and DoToEachFolder are completed, DoToSelFolder is called with
  106. //    a reference to the folder the user selected or dropped.
  107. //
  108. /********************************************************************************/
  109.  
  110. void    DoToSelFolder( FSSpec folderSpec )
  111. {
  112.     gFolderCount++;
  113. }
  114.  
  115.  
  116.  
  117. /********************************************************************************/
  118. //
  119. //    DeInitializeStuff is a routine which is called after all the recursive stuff
  120. //    is done.  It allows us to close up any files opened, release memory, etc.
  121. //
  122. /********************************************************************************/
  123.  
  124. void    DeInitializeStuff( void )
  125. {
  126.     Str255    resultString;
  127.     unsigned long    totalCount;
  128.     Str255    fileCountString, folderCountString, totalCountString;
  129.     char    *tempStr;
  130.     
  131.     itoa( gFileCount, tempStr );
  132.     CtoP( tempStr, fileCountString );
  133.     
  134.     itoa( gFolderCount, tempStr );
  135.     CtoP( tempStr, folderCountString );
  136.     
  137.     totalCount = ( gFolderCount + gFileCount );
  138.     itoa( totalCount, tempStr );
  139.     CtoP( tempStr, totalCountString );
  140.     
  141.     ConcatPStrings( totalCountString, "\p items: ", resultString );
  142.     ConcatPStrings( resultString, fileCountString, resultString );
  143.     ConcatPStrings( resultString, "\p files and ", resultString );
  144.     ConcatPStrings( resultString, folderCountString, resultString );
  145.     ConcatPStrings( resultString, "\p folders", resultString );
  146.     
  147.     DisplayAlert( resultString );
  148. }
  149.  
  150.  
  151. /********************************************************************************/
  152. //
  153. //    Misc. routines to support what this app needs to do go below here
  154. //
  155. /********************************************************************************/
  156.  
  157.  
  158. //
  159. //    Simplistic routine which reverses the order of characters in a string.
  160. //    Taken from K&R C book.
  161. //
  162. void    reverse( char s[] )
  163. {
  164.     long c, i, j;
  165.     for ( i = 0, j = strlen(s) - 1; i < j; i++, j-- )
  166.     {
  167.         c = s[i];
  168.         s[i] = s[j];
  169.         s[j] = c;
  170.     }
  171. }
  172.  
  173.  
  174. //
  175. //    Simplistic routine which converts a number into a zero-terminated string.
  176. //    Taken from K&R C book.
  177. //
  178. void    itoa( long n, char s[] )
  179. {
  180.     long    i, sign;
  181.     if ( ( sign = n ) < 0 )
  182.         n = -n;
  183.     i = 0;
  184.     do
  185.     {
  186.         s[i++] = n % 10 + '0';
  187.     } while ( ( n /= 10 ) > 0 );
  188.     if ( sign < 0 )
  189.         s[i++] = '-';
  190.     s[i] = '\0';
  191.     reverse(s);
  192. }
  193.  
  194.  
  195. //
  196. //    Quickie version of strlen (because that's all I need from the string.h library)
  197. //    Taken from K&R C book.
  198. //
  199. long    strlen(char *str)
  200. {
  201.     char *ptr = str;
  202.     
  203.     while (*ptr != '\0')
  204.         ptr++;
  205.     return ptr - str;
  206. }
  207.  
  208.  
  209. //
  210. //    Extremely rudimentary version of a routine which converts zero-terminated
  211. //    strings to Pascal strings.
  212. //
  213. void CtoP( char* cString, Str255 pString )
  214. {
  215.     short    length, i, j;
  216.     
  217.     length = strlen( cString );
  218.     
  219.     for ( i = 0, j = 1; i <= length; i++, j++ )
  220.         pString[j] = cString[i];
  221.     
  222.     pString[0] = length;
  223. }
  224.  
  225.  
  226. //
  227. //    Extremely rudimentary version of a routine which converts Pascal Strings
  228. //    to zero-terminated strings.
  229. //
  230. void PtoC( Str255 pString, char* cString )
  231. {
  232.     short length, i, j;
  233.     
  234.     length = pString[0];
  235.     
  236.     for ( i = 1, j = 0; i <= length; i++, j++ )
  237.         cString[j] = pString[i];
  238.     
  239.     cString[length] = '\0';
  240. }